package time

import (
	"fmt"
	"time"
)

func Time_show() {
	now := time.Now()
	fmt.Printf("now: %v\n", now)

	year := now.Year()
	month := now.Month()
	day := now.Day()
	hour := now.Hour()
	minute := now.Minute()
	second := now.Second()
	fmt.Printf("%d-%02d-%02d %02d:%02d:%2d \n", year, month, day, hour, minute, second)
	fmt.Printf("%T,%T,%T,%T,%T,%T,%T \n", now, year, month, day, hour, minute, second)
	// 时间戳 毫秒
	fmt.Printf("Timestamp type: %T, Timestamp: %v \n", now.Unix(), now.Unix())
	// 时间戳  纳秒
	fmt.Printf("Timestamp type: %T, Timestamp: %v \n", now.UnixNano(), now.UnixNano())

	timestamp := time.Now().Unix()
	timeObj := time.Unix(timestamp, 0)
	year = timeObj.Year()
	month = timeObj.Month()
	day = timeObj.Day()
	hour = timeObj.Hour()
	minute = timeObj.Minute()
	second = timeObj.Second()
	fmt.Printf("%d-%02d-%02d %02d:%02d:%2d \n", year, month, day, hour, minute, second)

}

/*
相加相减不能操作年月日 只能操作下面单位
	const (
	Nanosecond  Duration = 1
	Microsecond          = 1000 * Nanosecond
	Millisecond          = 1000 * Microsecond
	Second               = 1000 * Millisecond
	Minute               = 60 * Second
	Hour                 = 60 * Minute
)
*/
func AddTime(h, m, s, mls, mcs, ns time.Duration) {
	now := time.Now()
	fmt.Println(now.Add(time.Hour*h + time.Minute*m + time.Second*s + time.Millisecond*mls + time.Microsecond*mcs + time.Nanosecond*ns))
}

// 两个时间的差值 是否相等 之前之后
func OpTime() {
	now := time.Now()
	targetTime := now.Add(time.Hour)
	fmt.Println(targetTime.Sub(now))
	fmt.Println(targetTime.Equal(now))
	fmt.Println(now.Before(targetTime))
	fmt.Println(now.After(targetTime))
}

func Tick() {
	//每秒定时定性ticker
	ticker := time.Tick(time.Second)
	for v := range ticker {
		fmt.Println(v)
	}
}

func TimeFormatStr() {
	now := time.Now()
	// 格式化模板为Go的出生日期  2006年1月2日15点04分 Mon Jan
	// 24小时 yyyy-MM-dd hh24:mi:ss
	fmt.Println(now.Format("2006-01-02 15:04:05.000 Mon Jan"))
	// 12小时
	fmt.Println(now.Format("2006-01-02 03:04:05.000 PM Mon Jan"))
	fmt.Println(now.Format("2006/01/02 15:04"))
	fmt.Println(now.Format("15:04 2006/01/02"))
	fmt.Println(now.Format("2006/01/02"))
}

func StrFormatTime(str string) {
	now := time.Now()
	fmt.Println(now)
	// 加载时区
	loc, err := time.LoadLocation("Asia/Shanghai")
	if err != nil {
		fmt.Println(err)
		return
	}

	timeObj, err2 := time.ParseInLocation("2006/01/02 15:04:05", str, loc)
	if err2 != nil {
		fmt.Println(err2)
		return
	}
	fmt.Println(timeObj)
	fmt.Println(timeObj.Sub(now))
}
